package cn.edu.swufe.stu2025.finalwork;

import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import java.util.ArrayList;
import java.util.List;

public class BacklogFragment extends Fragment {
    private RecyclerView recyclerView;
    private BacklogAdapter adapter;
    private List<BacklogItem> backlogList;
    private boolean hideCompleted = false;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.backlog, container, false);
        recyclerView = view.findViewById(R.id.backlog_recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

        backlogList = new ArrayList<>();
        for (int i=1;i<11;i++) {
            backlogList.add(new BacklogItem("待办事项"+i+"           ", false));
        }
        adapter = new BacklogAdapter(backlogList);
        recyclerView.setAdapter(adapter);

        Button deleteBtn = view.findViewById(R.id.btn_delete_backlog);
        deleteBtn.setVisibility(View.INVISIBLE);

        adapter.setMultiSelectListener(enabled -> {
            deleteBtn.setVisibility(enabled ? View.VISIBLE : View.INVISIBLE);
        });

        deleteBtn.setOnClickListener(v -> {
            new android.app.AlertDialog.Builder(getContext())
                    .setTitle("确认删除")
                    .setMessage("确定要删除选中的待办吗？")
                    .setPositiveButton("取消", null)
                    .setNegativeButton("删除", (dialog, which) -> adapter.deleteSelected())
                    .show();
        });

        // 单击编辑
        adapter.setOnItemClickListener(position -> {
            BacklogItem item = backlogList.get(position);
            android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(getContext());
            final android.widget.EditText editText = new android.widget.EditText(getContext());
            editText.setText(item.content);
            builder.setTitle("编辑待办")
                    .setView(editText)
                    .setPositiveButton("确定", (dialog, which) -> {
                        String newItem = editText.getText().toString();
                        if (!newItem.isEmpty()) {
                            item.content = newItem;
                            adapter.notifyDataSetChanged();
                        }
                    })
                    .setNegativeButton("取消", null)
                    .show();
        });

        // 新增“隐藏已完成”按钮
        Button hideBtn = view.findViewById(R.id.btn_hide_completed);
        hideBtn.setOnClickListener(v -> {
            hideCompleted = !hideCompleted;
            adapter.setHideCompleted(hideCompleted);
            hideBtn.setText(hideCompleted ? "显示已完成" : "隐藏已完成");
        });

        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        Button addBtn = view.findViewById(R.id.btn_add_backlog);
        addBtn.setOnClickListener(v -> {
            android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(getContext());
            final android.widget.EditText editText = new android.widget.EditText(getContext());
            builder.setTitle("新建待办")
                    .setView(editText)
                    .setPositiveButton("确定", (dialog, which) -> {
                        String newItem = editText.getText().toString();
                        if (!newItem.isEmpty()) {
                            backlogList.add(0, new BacklogItem(newItem, false));
                            adapter.notifyDataSetChanged();
                        }
                    })
                    .setNegativeButton("取消", null)
                    .show();
        });
    }
}